package view.render.weapon
{
	import enums.main.UISize;
	
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	
	import model.vo.ItemVo;
	import model.vo.weapon.BugStickBugVo;
	
	import starling.display.Image;
	import starling.events.Event;
	
	import util.AtlasTextureGenerator;
	import util.WeaponUtil;
	
	import view.event.ItemEvent;
	import view.render.BaseItemScrollerItem;
	
	
	/**
	 * 操虫棍-猎虫培养列表item
	 * @author £iang
	 * 
	 */	
	public class BugStickBugItem extends BaseItemScrollerItem
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 猎虫名信息 */
		public var nameLabel:Label;
		/** 升级点数信息 */
		public var pointLabel:Label;
		/** 发动技能点数信息 */
		public var skillPointLabel:Label;
		/** 发动技能信息 */
		public var skillLabel:Label;
		/** 升级素材信息 */
		public var itemList:List;
		/** 打击猎虫示意图 */
		public var beatImage:Image;
		/** 切断猎虫示意图 */
		public var cutImage:Image;
		/** 调整组件用标签 */
		public var dummyLabel:Label;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** itemList数据 */
		private var _listDataItem:ListCollection;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initializeOther():void
		{
			super.initializeOther();
			nameLabel = new Label();
			nameLabel.textRendererProperties.isHTML = true;
			scollerContainer.addChild( nameLabel );
			
			pointLabel = new Label();
			scollerContainer.addChild( pointLabel );
			
			skillPointLabel = new Label();
			scollerContainer.addChild( skillPointLabel );
			
			skillLabel = new Label();
			skillLabel.textRendererProperties.isHTML = true;
			scollerContainer.addChild( skillLabel );
			
			itemList = new List();
			scollerContainer.addChild( itemList );
			itemList.width = UISize.MAIN_W * 0.8;
			itemList.itemRendererProperties.labelField  = "label";
			itemList.itemRendererProperties.iconField = "icon";
			itemList.itemRendererFactory = itemRenderFactroy;
			itemList.hasElasticEdges = false;
			itemList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
		
			beatImage = new Image( AtlasTextureGenerator.getBugIconTextureById( 1 ) );
			beatImage.scaleX = beatImage.scaleY = UISize.DPI_SCALE;
			scollerContainer.addChild( beatImage );
			
			cutImage = new Image( AtlasTextureGenerator.getBugIconTextureById( 1 ) );
			cutImage.scaleX = cutImage.scaleY = UISize.DPI_SCALE;
			scollerContainer.addChild( cutImage );
			
			dummyLabel = new Label();
			scollerContainer.addChild( dummyLabel );
		}
		
		/**
		 * 调整组件 
		 * 
		 */		
		override protected function draw():void
		{
			var labelY:Number = ( _itemH - 86 ) / 2 * UISize.DPI_SCALE;
			var labelY2:Number = ( _itemH - 43 ) / 2 * UISize.DPI_SCALE;
			var imageY:Number = ( _itemH - 80 ) / 2 * UISize.DPI_SCALE;
			var leftPadding:Number = UISize.TITLE_PADDING_LEFT * UISize.DPI_SCALE;
			var firstLabelFullW:Number;
			
			firstLabel.x = leftPadding;
			firstLabelFullW = UISize.MAIN_W * _firstPercent;
			
			nameLabel.y = labelY;
			nameLabel.x = 0;
			nameLabel.width  = UISize.MAIN_W * 0.6;
			
			pointLabel.y = labelY2;
			pointLabel.x = UISize.MAIN_W * 0.6;
			pointLabel.width  = UISize.MAIN_W * 0.2;
			
			skillPointLabel.y = labelY2;
			skillPointLabel.x = UISize.MAIN_W * 0.8;
			skillPointLabel.width  = UISize.MAIN_W * 0.4;
			
			skillLabel.y = labelY;
			skillLabel.x = UISize.MAIN_W * 1.2;
			skillLabel.width  = UISize.MAIN_W * 0.4;
			
			itemList.x = UISize.MAIN_W * 1.6;
			
			beatImage.x = UISize.MAIN_W * 2.4;
			beatImage.y = imageY;
			
			cutImage.x = UISize.MAIN_W * 2.4 + 100 * UISize.DPI_SCALE;
			cutImage.y = imageY;
			
			dummyLabel.x = UISize.MAIN_W * 2.4;
			dummyLabel.width = UISize.MAIN_W * 0.8;
			
			scollerContainer.x = firstLabelFullW;
			scollerContainer.height = _itemH * UISize.DPI_SCALE;
			scollerContainer.width = UISize.MAIN_W - firstLabelFullW;
			
			this.width = UISize.MAIN_W;
			this.height = _itemH * UISize.DPI_SCALE;
		}
		
		/**
		 * 设置item数据 
		 * @param data 数据
		 * 
		 */		
		public function setData( data:BugStickBugVo ):void
		{
			firstLabel.text = "Lv" + data.level;
			firstLabel.color = WeaponUtil.getRareColor( data.level );
			nameLabel.text = data.beatName + "(打)<br/>" + data.cutName + "(切)";
			pointLabel.text = data.point;
			skillPointLabel.text = data.skillPoint;
			skillLabel.text = WeaponUtil.getBugSkillText( data.skill );
			skillLabel.y = skillLabel.text.search( "br" ) >= 0 ?
				( _itemH - 86 ) / 2 * UISize.DPI_SCALE :
				( _itemH - 43 ) / 2 * UISize.DPI_SCALE;
			
			var len:int;
			var i:int;
			var tmpH:Number;
			
			_listDataItem = new ListCollection([]);
			len = data.item.length;
			
			for ( i = 0; i < len; i ++ )
			{
				_listDataItem.push( mapItemVo2ListObj( data.item[ i ] as ItemVo, data.itemNum[ i ] ) );
			}
			
			itemList.height = tmpH = UISize.ITEM_LIST_ITEM_H * 
				UISize.DPI_SCALE * len;
			itemList.y = ( _itemH * UISize.DPI_SCALE - tmpH ) / 2;
			itemList.dataProvider = _listDataItem;
			
			beatImage.texture.dispose();
			beatImage.texture = AtlasTextureGenerator.getBugIconTextureById( data.beatImage );
			cutImage.texture.dispose();
			cutImage.texture = AtlasTextureGenerator.getBugIconTextureById( data.cutImage );
		}
		
		/**
		 * itemLowRenderer皮肤 
		 * @return 
		 * 
		 */		
		private function itemRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = commonItem();
			renderer.addEventListener( Event.TRIGGERED, itemTriggeredHandler );
			return renderer;
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理下位道具item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemTriggeredHandler( event:Event ):void
		{
			var itemId:int =  _listDataItem.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			this.parent.parent.parent.parent.dispatchCommonEvent( itemEvent );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override protected function disposeAnythingInContainer():void
		{
			disposeLabels( [
				"name", "point",
				"skillPoint", "skill"
			] );
			
			beatImage.dispose();
			beatImage.texture.dispose();
			beatImage = null;
			cutImage.dispose();
			cutImage.texture.dispose();
			cutImage = null;
			
			disposeItemListData( _listDataItem );
			_listDataItem = null;
			
			itemList.dispose();
			itemList = null;
			
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BugStickBugItem()
		{
			super();
			_itemH = UISize.BUG_ITEM_H;
			_firstPercent = 0.2;
			initialize();
		}
	}
}